{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.9 与时间相关的操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.时间的物理量和对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:53.654674Z",
     "end_time": "2024-05-09T21:31:53.785838Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2024, 5, 9, 21, 31, 53, 654674)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime\n",
    "\n",
    "now = datetime.datetime.now()\n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "(9, 5, 2024)"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.day, now.month, now.year"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:53.670703Z",
     "end_time": "2024-05-09T21:31:53.785838Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2024, 5, 9, 21, 31, 53, 686712)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.today()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:53.686711Z",
     "end_time": "2024-05-09T21:31:53.785838Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2024-05-09 21:31:54.183511')"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "now = pd.Timestamp.now()\n",
    "now"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:53.704484Z",
     "end_time": "2024-05-09T21:31:54.220804Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "(2024, 5, 9)"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.year, now.month, now.day"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.224741Z",
     "end_time": "2024-05-09T21:31:54.285299Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pytz\n",
    "\n",
    "brussels_tz = pytz.timezone('Europe/Brussels')\n",
    "shanghai_tz = pytz.timezone('Asia/Shanghai')\n",
    "now_brussels = datetime.datetime.now(tz=brussels_tz)\n",
    "now_shanghai = now_brussels.replace(tzinfo=shanghai_tz)\n",
    "now_shanghai == now_brussels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.253200Z",
     "end_time": "2024-05-09T21:31:54.336453Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2024, 5, 9, 15, 31, 54, 320169, tzinfo=<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_shanghai"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.336453Z",
     "end_time": "2024-05-09T21:31:54.419378Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(2024, 5, 9, 15, 31, 54, 320169, tzinfo=<DstTzInfo 'Europe/Brussels' CEST+2:00:00 DST>)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_brussels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.369329Z",
     "end_time": "2024-05-09T21:31:54.425377Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2024-05-09 21:31:54.387397+0800', tz='Asia/Shanghai')"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_shanghai = pd.Timestamp.now(tz=\"Asia/Shanghai\")\n",
    "now_shanghai"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.387397Z",
     "end_time": "2024-05-09T21:31:54.425377Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2024-05-09 15:31:54.387397+0200', tz='Europe/Brussels')"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_brussels = now_shanghai.tz_convert(\"Europe/Brussels\")\n",
    "now_brussels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.403603Z",
     "end_time": "2024-05-09T21:31:54.425377Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_shanghai == now_brussels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.423876Z",
     "end_time": "2024-05-09T21:31:54.503775Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1997-07-01 00:00:00')"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(datetime.datetime(1997, 7, 1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.446458Z",
     "end_time": "2024-05-09T21:31:54.504775Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1997-07-01 00:00:00')"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(\"1997-7-1\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.465024Z",
     "end_time": "2024-05-09T21:31:54.504775Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1997-07-01 00:00:00')"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(1997, 7, 1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.495445Z",
     "end_time": "2024-05-09T21:31:54.511845Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1677-09-21 00:12:43.145224193')"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp.min"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.508802Z",
     "end_time": "2024-05-09T21:31:54.590283Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2262-04-11 23:47:16.854775807')"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp.max"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.533384Z",
     "end_time": "2024-05-09T21:31:54.623826Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "Timedelta('9809 days 21:31:54.387397')"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start = pd.Timestamp(\"1997-7-1\", tz='Asia/Shanghai')\n",
    "delta = now_shanghai - start\n",
    "delta"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.558040Z",
     "end_time": "2024-05-09T21:31:54.623826Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2051-03-19 19:03:48.774794+0800', tz='Asia/Shanghai')"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "future = now_shanghai + delta\n",
    "future"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.574547Z",
     "end_time": "2024-05-09T21:31:54.623826Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "Timedelta('3 days 04:05:06')"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timedelta(days=3, hours=4, minutes=5, seconds=6)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.590283Z",
     "end_time": "2024-05-09T21:31:54.664744Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "Timedelta('1 days 10:17:36')"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timedelta(seconds=123456)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.623826Z",
     "end_time": "2024-05-09T21:31:54.664744Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('2024-05-06/2024-05-12', 'W-SUN')"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_week = pd.Period.now(freq=\"W\")\n",
    "now_week"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.643292Z",
     "end_time": "2024-05-09T21:31:54.664744Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "(Timestamp('2024-05-06 00:00:00'), Timestamp('2024-05-12 23:59:59.999999999'))"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now_week.start_time, now_week.end_time"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.664261Z",
     "end_time": "2024-05-09T21:31:54.681107Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('1997-07', 'M')"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period(\"1997-07\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.681107Z",
     "end_time": "2024-05-09T21:31:54.742027Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('1997-07-01', 'D')"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period(\"1997-07\", freq=\"D\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.701741Z",
     "end_time": "2024-05-09T21:31:54.742027Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('1997-06-30/1997-07-06', 'W-SUN')"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = pd.Timestamp(\"1997-07-01\")\n",
    "t1.to_period(\"W\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.721380Z",
     "end_time": "2024-05-09T21:31:54.742027Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "(2024, 5, 9)"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = pd.Timestamp.now()\n",
    "now.year, now.month, now.day"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.742027Z",
     "end_time": "2024-05-09T21:31:54.793857Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "130"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.dayofyear"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.757834Z",
     "end_time": "2024-05-09T21:31:54.820288Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "3"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.dayofweek"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.777966Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "21"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.hour"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.793857Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.比较相关模块"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(1997, 7, 1, 0, 0)"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime(year=1997, month=7, day=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.826705Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(1997, 7, 1, 0, 0)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dateutil import parser\n",
    "\n",
    "date = parser.parse(\"1st of July, 1997\")\n",
    "date"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.843114Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "datetime.datetime(1997, 7, 1, 0, 0)"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parser.parse(\"1997.07.01\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.861976Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "'Tuesday'"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.strftime('%A')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.881427Z",
     "end_time": "2024-05-09T21:31:54.908087Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "array('1997-07-01', dtype='datetime64[D]')"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_np = np.array('1997-07-01', dtype=np.datetime64)\n",
    "date_np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.908087Z",
     "end_time": "2024-05-09T21:31:54.924979Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "(numpy.datetime64('1997-07'), dtype('<M8[M]'))"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = np.datetime64(\"1997-07\")\n",
    "m, m.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.924979Z",
     "end_time": "2024-05-09T21:31:55.040782Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "(numpy.datetime64('1997-07-01T19:19:00'), dtype('<M8[s]'))"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.datetime64(\"1997-07-01 19:19:00\")\n",
    "n, n.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.948315Z",
     "end_time": "2024-05-09T21:31:55.044891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "(numpy.datetime64('1997-07-01'), dtype('<M8[D]'))"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.datetime64(\"1997-07-01 19:19:00\", 'D')\n",
    "n, n.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.970932Z",
     "end_time": "2024-05-09T21:31:55.044891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "(numpy.datetime64('1997-07-01T19:19:00.000000000'), dtype('<M8[ns]'))"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.datetime64(\"1997-07-01 19:19:00\", 'ns')\n",
    "n, n.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:54.998186Z",
     "end_time": "2024-05-09T21:31:55.044891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['1997-07-01', '1997-07-02', '1997-07-03', '1997-07-04',\n       '1997-07-05', '1997-07-06', '1997-07-07', '1997-07-08',\n       '1997-07-09', '1997-07-10', '1997-07-11', '1997-07-12',\n       '1997-07-13', '1997-07-14', '1997-07-15', '1997-07-16',\n       '1997-07-17', '1997-07-18', '1997-07-19', '1997-07-20'],\n      dtype='datetime64[D]')"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_np + np.arange(20)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.024456Z",
     "end_time": "2024-05-09T21:31:55.044891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1997-07-01 00:00:00')"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_pd = pd.to_datetime(\"1st of July, 1997\")\n",
    "date_pd"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.044391Z",
     "end_time": "2024-05-09T21:31:55.062237Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "'Tuesday'"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_pd.strftime('%A')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.062237Z",
     "end_time": "2024-05-09T21:31:55.164369Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1997-07-01', '1997-07-02', '1997-07-03', '1997-07-04',\n               '1997-07-05', '1997-07-06', '1997-07-07', '1997-07-08',\n               '1997-07-09', '1997-07-10', '1997-07-11', '1997-07-12',\n               '1997-07-13', '1997-07-14', '1997-07-15', '1997-07-16',\n               '1997-07-17', '1997-07-18', '1997-07-19', '1997-07-20'],\n              dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_pd + pd.to_timedelta(np.arange(20), 'D')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.078564Z",
     "end_time": "2024-05-09T21:31:55.164871Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.时间索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "1997-07-01    100\n1997-07-02    105\n1997-07-03    110\ndtype: int64"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datas = [pd.Timestamp('1997-7-1'), pd.Timestamp('1997-7-2'), pd.Timestamp('1997-7-3')]\n",
    "s = pd.Series([100, 105, 110], index=datas)\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.094899Z",
     "end_time": "2024-05-09T21:31:55.164871Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1997-07-01', '1997-07-02', '1997-07-03'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.111555Z",
     "end_time": "2024-05-09T21:31:55.164871Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1997-07-01', '1997-07-03', '1997-07-05', '1997-07-07',\n               '1997-07-08', '1997-07-09', '1997-10-07'],\n              dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_index = pd.to_datetime([datetime.datetime(1997, 7, 1), '3rd of July, 1997', '1997.7.5',\n",
    "                             '1997-07-07', '1997-Jul-8', '19970709', '10/7/1997'])\n",
    "date_index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.131911Z",
     "end_time": "2024-05-09T21:31:55.164871Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "1997-07    100.000000\n1997-08    133.333333\n1997-09    166.666667\n1997-10    200.000000\nFreq: M, dtype: float64"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periods = [pd.Period('1997-07'), pd.Period('1997-08'), pd.Period('1997-09'), pd.Period('1997-10')]\n",
    "ps = pd.Series(np.linspace(100, 200, 4), index=periods)\n",
    "ps"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.148520Z",
     "end_time": "2024-05-09T21:31:55.164871Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "PeriodIndex(['1997-07', '1997-08', '1997-09', '1997-10'], dtype='period[M]')"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.164871Z",
     "end_time": "2024-05-09T21:31:55.185432Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "PeriodIndex(['1997-07-01', '1997-07-03', '1997-07-05', '1997-07-07',\n             '1997-07-08', '1997-07-09', '1997-10-07'],\n            dtype='period[D]')"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "period_index = date_index.to_period(\"D\")\n",
    "period_index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.185432Z",
     "end_time": "2024-05-09T21:31:55.300340Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "PeriodIndex(['1997-07', '1997-07', '1997-07', '1997-07', '1997-07', '1997-07',\n             '1997-10'],\n            dtype='period[M]')"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "period_index_m = date_index.to_period(\"M\")\n",
    "period_index_m"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.205551Z",
     "end_time": "2024-05-09T21:31:55.343223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('1997-07-01 00:00:00')"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = date_index[0]\n",
    "t"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.225534Z",
     "end_time": "2024-05-09T21:31:55.343223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "TimedeltaIndex(['0 days', '2 days', '4 days', '6 days', '7 days', '8 days',\n                '98 days'],\n               dtype='timedelta64[ns]', freq=None)"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta_index = date_index - t\n",
    "delta_index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.246415Z",
     "end_time": "2024-05-09T21:31:55.343223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1997-07-01', '1997-07-02', '1997-07-03', '1997-07-04',\n               '1997-07-05', '1997-07-06', '1997-07-07', '1997-07-08',\n               '1997-07-09', '1997-07-10',\n               ...\n               '1997-10-23', '1997-10-24', '1997-10-25', '1997-10-26',\n               '1997-10-27', '1997-10-28', '1997-10-29', '1997-10-30',\n               '1997-10-31', '1997-11-01'],\n              dtype='datetime64[ns]', length=124, freq='D')"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"1997-7\", \"1997-11\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.268294Z",
     "end_time": "2024-05-09T21:31:55.343223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['1997-07-31', '1997-08-31', '1997-09-30', '1997-10-31'], dtype='datetime64[ns]', freq='M')"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"1997-7\", \"1997-11\", freq=\"M\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.284519Z",
     "end_time": "2024-05-09T21:31:55.343223Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2017-07-01 00:00:00', '2017-07-01 01:00:00',\n               '2017-07-01 02:00:00', '2017-07-01 03:00:00',\n               '2017-07-01 04:00:00', '2017-07-01 05:00:00',\n               '2017-07-01 06:00:00', '2017-07-01 07:00:00'],\n              dtype='datetime64[ns]', freq='H')"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"2017-7-1\", periods=8, freq=\"H\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.304620Z",
     "end_time": "2024-05-09T21:31:55.441952Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "PeriodIndex(['2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',\n             '2018-01'],\n            dtype='period[M]')"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.period_range(\"2017-7\", periods=7, freq=\"M\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.323436Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days', '5 days',\n                '6 days'],\n               dtype='timedelta64[ns]', freq='D')"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.timedelta_range(0, periods=7)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.343223Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "TimedeltaIndex(['0 days 00:00:00', '0 days 03:30:00', '0 days 07:00:00',\n                '0 days 10:30:00', '0 days 14:00:00', '0 days 17:30:00',\n                '0 days 21:00:00'],\n               dtype='timedelta64[ns]', freq='210T')"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.timedelta_range(0, periods=7, freq=\"3H30T\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.359520Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2017-10-02', '2017-10-03', '2017-10-04', '2017-10-05',\n               '2017-10-06', '2017-10-09', '2017-10-10', '2017-10-11',\n               '2017-10-12'],\n              dtype='datetime64[ns]', freq='B')"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"2017-9-30\", periods=9, freq=pd.tseries.offsets.BDay())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.389658Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.重采样"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2017-07-20 00:00:00')"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dindex = pd.date_range(\"20/7/2017\", periods=50, freq=\"D\")\n",
    "sdata = pd.Series(np.random.randn(len(dindex)), index=dindex)\n",
    "sdata.index[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.405890Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2017-09-07 00:00:00')"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata.index[-1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.425571Z",
     "end_time": "2024-05-09T21:31:55.442710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-07-31   -0.233175\n2017-08-31    0.090118\n2017-09-30   -0.368776\nFreq: M, dtype: float64"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata.resample('M').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.442710Z",
     "end_time": "2024-05-09T21:31:55.459008Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00    21\n2017-11-10 00:07:00    70\n2017-11-10 00:14:00    99\nFreq: 7T, dtype: int32"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t_index = pd.date_range(\"2017-11-10\", periods=20, freq=\"T\")\n",
    "sdata2 = pd.Series(np.arange(20), index=t_index)\n",
    "sdata2.resample('7min').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.459008Z",
     "end_time": "2024-05-09T21:31:55.567133Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-09 23:53:00     0\n2017-11-10 00:00:00    28\n2017-11-10 00:07:00    77\n2017-11-10 00:14:00    85\nFreq: 7T, dtype: int32"
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.resample('7min', closed='right').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.475705Z",
     "end_time": "2024-05-09T21:31:55.570461Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00     0\n2017-11-10 00:07:00     7\n2017-11-10 00:14:00    14\nFreq: 7T, dtype: int32"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.asfreq('7min')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.495856Z",
     "end_time": "2024-05-09T21:31:55.571458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00     0.0\n2017-11-10 00:00:01     NaN\n2017-11-10 00:00:02     NaN\n2017-11-10 00:00:03     NaN\n2017-11-10 00:00:04     NaN\n                       ... \n2017-11-10 00:18:56     NaN\n2017-11-10 00:18:57     NaN\n2017-11-10 00:18:58     NaN\n2017-11-10 00:18:59     NaN\n2017-11-10 00:19:00    19.0\nFreq: S, Length: 1141, dtype: float64"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.asfreq('S')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.521359Z",
     "end_time": "2024-05-09T21:31:55.571458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00     0\n2017-11-10 00:00:01     1\n2017-11-10 00:00:02     1\n2017-11-10 00:00:03     1\n2017-11-10 00:00:04     1\n                       ..\n2017-11-10 00:18:56    19\n2017-11-10 00:18:57    19\n2017-11-10 00:18:58    19\n2017-11-10 00:18:59    19\n2017-11-10 00:19:00    19\nFreq: S, Length: 1141, dtype: int32"
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.asfreq('S', method='bfill')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.540871Z",
     "end_time": "2024-05-09T21:31:55.571458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00     0\n2017-11-10 00:00:01     0\n2017-11-10 00:00:02     0\n2017-11-10 00:00:03     0\n2017-11-10 00:00:04     0\n                       ..\n2017-11-10 00:18:56    18\n2017-11-10 00:18:57    18\n2017-11-10 00:18:58    18\n2017-11-10 00:18:59    18\n2017-11-10 00:19:00    19\nFreq: S, Length: 1141, dtype: int32"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.asfreq('S', method='ffill')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.550386Z",
     "end_time": "2024-05-09T21:31:55.571458Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-11-10 00:00:00      0\n2017-11-10 00:00:01    999\n2017-11-10 00:00:02    999\n2017-11-10 00:00:03    999\n2017-11-10 00:00:04    999\n                      ... \n2017-11-10 00:18:56    999\n2017-11-10 00:18:57    999\n2017-11-10 00:18:58    999\n2017-11-10 00:18:59    999\n2017-11-10 00:19:00     19\nFreq: S, Length: 1141, dtype: int32"
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata2.asfreq('S', fill_value=999)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.570461Z",
     "end_time": "2024-05-09T21:31:55.605711Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [
    {
     "data": {
      "text/plain": "2017-09-01   -0.393143\n2017-09-02   -0.222833\n2017-09-03   -0.049598\n2017-09-04   -0.407574\n2017-09-05   -1.082895\n2017-09-06   -1.861980\n2017-09-07    1.436589\nFreq: D, dtype: float64"
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdata['2017-09']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.592488Z",
     "end_time": "2024-05-09T21:31:55.663125Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T21:31:55.606214Z",
     "end_time": "2024-05-09T21:31:55.679637Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
